import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import viteRequire from 'vite-plugin-require'

export default defineConfig({
  plugins: [
    vue({
      // 启用兼容模式，使Vue 2语法在Vue 3中工作
      template: {
        compilerOptions: {
          compatConfig: {
            MODE: 2 // 启用Vue 2兼容模式
          }
        }
      }
    }),
    viteRequire()
  ],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
      'vue': '@vue/compat',
      '@vue/compat': 'node_modules/@vue/compat/dist/vue.esm-bundler.js',
      // 处理core-js兼容
      'core-js/library/fn/object/assign': 'core-js/es/object/assign',
      'core-js/library/fn/symbol': 'core-js/es/symbol',
      'core-js/library/fn/symbol/iterator': 'core-js/es/symbol/iterator',
      'element-theme-17B3A3': path.resolve(__dirname, './src/element-ui-theme/element-#17B3A3'),
      'element-theme-3E8EF7': path.resolve(__dirname, './src/element-ui-theme/element-#3E8EF7'),
      'element-ui': 'element-plus',
      'qs': 'qs/dist/qs.js' // 如果需要明确指定路径
    },
    extensions: ['.js', '.vue', '.json','.scss'], // 添加自动解析的扩展名
  },
  optimizeDeps: {
    include: [
      'core-js/es/object/assign',
      'core-js/es/symbol',
      'core-js/es/symbol/iterator'
    ],
    esbuildOptions: {
      target: 'es2015'
    },
    exclude: [
    ]
  },
  server: {
    port: 8080, // 设置开发服务器端口
    open: true, // 自动打开浏览器
  },
  css: {
    preprocessorOptions: {
      scss: {
        quietDeps: true,
        implementation: require('sass'),
        additionalData: `
          @use "sass:color";
          @use "@/assets/scss/variables" as *;
        `,
      }
    }
  },
  build: {
    outDir: 'dist', // 输出目录
    assetsDir: 'assets', // 静态资源目录
    minify: 'terser', // 混淆器
    terserOptions: {
      compress: {
        drop_console: true, // 移除console
        drop_debugger: true // 移除debugger
      }
    },
    rollupOptions: {
      external: [
        // 排除 UEditor 资源
      ]
    }
  }
})
